<?php
class CouponParser{

	public static function Instance(){
		return new CouponParser();
	}


	public function parseCouponResultToXml($result){
		if ($result instanceof Model_Coupon){
			$entity = $this->parseSupplier($result);
		}else{
			$result_array = array();
			foreach ($result as $item){
				array_push($result_array, $this->parseCoupon($item));
			}
			$entity = new Entity();
			$entity->entityName = "Coupon";
			$entity->entityValue = $result_array;
		}

		$xml = XmlUtil::Factory()->entityToXml($entity,0,0);
		return $xml;
	}

	private function parseCoupon($item){
		$itemArray['Id'] = $item->id;
		$itemArray['Group'] = $item->group;
		if (!empty($item->smallpicture)){
			if (file_exists(realpath($this->defaultImgPath().$item->smallpicture))){
				$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
				$itemArray['SmallImgUrl'] = URL::domain().COUPON_IMG_PATH.$item->smallpicture;
			}else{
				$itemArray['SmallImgUrl'] = "";
			}
		}else{
			$itemArray['SmallImgUrl'] = "";
		}
		if (!empty($item->largepicture)){
			if (file_exists(realpath($this->defaultImgPath().$item->largepicture))){
				$itemArray['LargeImgUrl'] = URL::domain().COUPON_IMG_PATH.$item->largepicture;
			}else{
				$itemArray['LargeImgUrl'] = "";
			}
		}else{
			$itemArray['LargeImgUrl'] = "";
		}
		$itemArray['Sort'] = $item->sort;
		$itemArray['Desc'] = $item->desc;
		$itemArray['CreateTime'] = $item->createtime;
		$itemArray['UpdateTime'] = $item->updatetime;
		$itemArray['Height'] = $item->height;
		$itemArray['Width'] = $item->width;
		$entity = new Entity();
		$entity->entityName = "Coupon";
		$entity->entityValue = $itemArray;
		return $entity;
	}
	
	private function defaultImgPath(){
		return $_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR.Upload::$default_directory.DIRECTORY_SEPARATOR.'coupon'.DIRECTORY_SEPARATOR;
	}
}